﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Autodesk.Revit.DB;

namespace SectionSignAndFilled.MaterialManager
{
	internal class OneScale
	{
		public FillPattern GetFillPattern()
		{
			FillPattern result = null;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.document);
			List<Element> list = (from e in filteredElementCollector.OfClass(typeof(FillPatternElement))
			where e.Name == this.NameModify
			select e).ToList<Element>();
			if (list.Count != 0)
			{
				FillPatternElement fillPatternElement = list[0] as FillPatternElement;
				result = fillPatternElement.GetFillPattern();
			}
			return result;
		}

		public FillPatternElement GetFillPatternElement()
		{
			FillPatternElement result = null;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.document);
			List<Element> list = (from e in filteredElementCollector.OfClass(typeof(FillPatternElement))
			where e.Name == this.NameModify
			select e).ToList<Element>();
			if (list.Count != 0)
			{
				result = (list[0] as FillPatternElement);
			}
			return result;
		}

		public bool WriteData(XmlDocument xmlDoc, XmlNode root, XmlNodeList childNodes)
		{
			XmlNode xmlNode = XMLTools.FindNodeByAtt(childNodes, "Scale", this.Scale.ToString());
			if (xmlNode == null)
			{
				xmlNode = xmlDoc.CreateElement("OneScale");
				(xmlNode as XmlElement).SetAttribute("Scale", this.Scale.ToString());
				xmlNode = root.AppendChild(xmlNode);
			}
			XmlElement xmlElement = xmlNode as XmlElement;
			xmlElement.SetAttribute("NameModify", this.NameModify);
			xmlElement.SetAttribute("ColorModifyR", this.ColorModify.Red.ToString());
			xmlElement.SetAttribute("ColorModifyG", this.ColorModify.Green.ToString());
			xmlElement.SetAttribute("ColorModifyB", this.ColorModify.Blue.ToString());
			return true;
		}

		public bool ReadData(XmlNodeList childNodes)
		{
			XmlElement xmlElement = null;
			for (int i = 0; i < childNodes.Count; i++)
			{
				XmlElement xmlElement2 = childNodes[i] as XmlElement;
				string attribute = xmlElement2.GetAttribute("Scale");
				double num = Convert.ToDouble(attribute);
				if (Math.Abs(num - this.Scale) < 1E-06)
				{
					xmlElement = xmlElement2;
					break;
				}
			}
			if (xmlElement == null)
			{
				return false;
			}
			this.NameModify = xmlElement.GetAttribute("NameModify");
			string attribute2 = xmlElement.GetAttribute("ColorModifyR");
			string attribute3 = xmlElement.GetAttribute("ColorModifyG");
			string attribute4 = xmlElement.GetAttribute("ColorModifyB");
			this.ColorModify = new Color(Convert.ToByte(attribute2), Convert.ToByte(attribute3), Convert.ToByte(attribute4));
			return true;
		}

		public Document document;

		public double Scale = 1.0;

		public string NameModify;

        public Autodesk.Revit.DB.Color ColorModify = new Autodesk.Revit.DB.Color(0, 0, 0);

		public int rowInGrid;

		public int colInGrid;
	}
}
